package Test6.LNode;

import Test6.LNode.Node;

public class LinkList {
    private Node head;

    public LinkList(){
        this.head=null;
    }

    public LinkList(Node node){
        this.head=node;
    }

    public Node getHead(){
        return this.head;
    }

    public int length(){
        if(this.head==null){
            return 0;
        }
        Node node=this.head;
        int count=0;
        while(node!=null){
            count++;
            node=node.getNext();
        }
        return count;
    }

   public void add(Object data) {
       Node node = new Node(data);
       if (this.head == null) {
           this.head = node;
       } else {
           Node tmp=this.head;
           while (tmp.getNext() != null) {
               tmp = tmp.getNext();
           }
           tmp.setNext(node);
       }
   }



    public boolean remove(Object data){
        //如果传空结点
        if(this.head==null){
            return false;
        }
        Node p=this.head;
        while(p!=null){
            if(p.getData().equals(data)){
                Node tmp=this.head;
                //找p的前驱结点
                while(tmp.getNext()!=p){
                    tmp=tmp.getNext();
                }
                tmp.setNext(p.getNext());
                p.setNext(null);
                return true;
            }
            p=p.getNext();
        }
        return false;
    }

    public Node search(Object data){
        if(this.head==null){
            return null;
        }
        Node tmp=this.head;
        while(tmp!=null){
            //System.out.println(tmp.getData()+":"+tmp.getData().getClass());
            if(tmp.getData()==data|| tmp.getData().equals(data)){
                return tmp;
            }
            tmp=tmp.getNext();
        }
        return null;
    }

    public boolean insertAfter(Node previous,Object data){
        if(this.head==null||previous==null){
            return false;
        }
        Node prt=new Node(data);
        prt.setNext(previous.getNext());
        previous.setNext(prt);
        return true;
    }

    public Node get(int index){
        if(this.head==null||index<0||index>this.length()-1){
            return null;
        }
        int count=0;
        Node node=this.head;
        while(count!=index){
            count++;
            node=node.getNext();
        }
        return node;
    }

    public boolean set(Node node,Object data){
        if(this.head==null||node==null){
            return false;
        }
        node.setData(data);
        return true;
    }
    @Override
    public String toString(){
        return head.toString();
    }



    public static void main(String[] args) {
            LinkList L=new LinkList();
            L.add(1);
        //System.out.println(L.length());
            L.add(2);
        //System.out.println(L.length());
            L.add(3);
        //System.out.println(L.length());
            System.out.println(L.toString());
            Node p=L.search(2);
        //System.out.println(p);
            p.setData(22);
        System.out.println(L.toString());
        L.insertAfter(p,23);
        System.out.println(L.toString());
        L.remove(22);
        System.out.println(L.toString());
            System.out.println(L.length());
        }

}
